package com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.impl

import com.minhua.jijiehao2.base.BaseMvpPresenter
import com.minhua.jijiehao2.bean.BaseBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.http.CallBack
import com.minhua.jijiehao2.http.ParamsMap
import com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.imp.IFinalForgetPasswordPresenter
import com.minhua.jijiehao2.mainframe.launcher.finalforgetpasswordactivity.imp.IFinalForgetPasswordView
import com.minhua.jijiehao2.utils.InputUtils
import com.minhua.jijiehao2.utils.T
import com.minhua.jijiehao2.utils.encryption.MDUtil

class FinalForgetPasswordPresenter : BaseMvpPresenter<IFinalForgetPasswordView>(), IFinalForgetPasswordPresenter {
    private val mFinalForgetPasswordModel = FinalForgetPasswordModel()

    override fun updatePassword() {
        val phone = v?.getUserName() ?: ""
        val code = v?.getCode() ?: ""
        val password = v?.getPassword() ?: ""
        val rePassword = v?.getRePassword() ?: ""

        if (!(InputUtils.checkPassword(password) && InputUtils.checkPassword(rePassword))) {
            T.showShort("密码由6~20为数字和字母组成")
            return
        }
        if (password != rePassword) {
            T.showShort("两次输入密码不一致")
            return
        }

        val map = ParamsMap()
        val signMsg = MDUtil.encode(MDUtil.TYPE.MD5, DefineKey.APP_KEY + MDUtil.encode(MDUtil.TYPE.MD5, password).toUpperCase() +
                phone + code)
        map.put(DefineKey.SIGN_MSG, signMsg)
        map.put(DefineKey.PHONE, phone)
        map.put(DefineKey.NEW_PASSWORD, MDUtil.encode(MDUtil.TYPE.MD5, password).toUpperCase())
        map.put(DefineKey.CHECK_CODE, code)
        mFinalForgetPasswordModel.updatePassword(map, object : CallBack<Any>() {
            override fun onSuccess(t: BaseBean<Any>) {
                v?.onUpdateSuccess()
            }
        })
    }

}